package org.i1510.spider.utils.cbj;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.i1510.common.utils.sqliteJdbc.DbUtils;
import org.i1510.spider.bean.SysSpiderRuleBean;
import org.i1510.spider.utils.cbj.worker.SpiderDescRuleWorker;
import org.i1510.spider.utils.spider.SpiderRuleRun2_an;
import org.i1510.spider.utils.spider.model.SpiderParams;
import org.i1510.spider.utils.spider.queue.SpiderRuleQueue;

/**
 * @author Administrator 执行爬虫规则引擎 http://caibaojian.com/c/code 
 */
public class SpiderDescRuleRun {
	/**
	*@Author Rambo
	*@Desc：测试
	*@param args TODO
	*@Version  SpiderRuleRun.java,v 1.1 2014-9-20 下午9:13:54 
	*/
	public static void main(String[] args) {
		DbUtils db00 = new DbUtils();
		List<Map> list = db00.executeQuery("select * from job_caibaojian where isFlag != 'X';"); //查询数据 
		SysSpiderRuleBean[] sysSpiderRuleBeans = new SysSpiderRuleBean[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Map map = list.get(i);
			sysSpiderRuleBeans[i] = new SysSpiderRuleBean();
			sysSpiderRuleBeans[i].setDocId((i+1)+"");
			sysSpiderRuleBeans[i].setKey(map.get("tag").toString());
			sysSpiderRuleBeans[i].setValue(map.get("title").toString());
			sysSpiderRuleBeans[i].setUrl(map.get("href").toString());
			
		}		
		SpiderDescRuleRun.run(sysSpiderRuleBeans);
		
		
	}
	public  void job(){
		DbUtils db00 = new DbUtils();
		List<Map> list = db00.executeQuery("select * from job_caibaojian where isFlag != 'X';"); //查询数据 
		SysSpiderRuleBean[] sysSpiderRuleBeans = new SysSpiderRuleBean[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Map map = list.get(i);
			sysSpiderRuleBeans[i] = new SysSpiderRuleBean();
			sysSpiderRuleBeans[i].setDocId((i+1)+"");
			sysSpiderRuleBeans[i].setKey(map.get("tag").toString());
			sysSpiderRuleBeans[i].setValue(map.get("title").toString());
			sysSpiderRuleBeans[i].setUrl(map.get("href").toString());
			
		}		
		SpiderDescRuleRun.run(sysSpiderRuleBeans);
	}
	public static void run(SysSpiderRuleBean[] sysSpiderRuleBeans) {
		// 初始化配置参数
		initializeParams();
		// 初始化爬取队列
		initializeQueue(sysSpiderRuleBeans);

		// 创建worker线程并启动
		for (int i = 1; i <= SpiderParams.WORKER_NUM; i++) {
			new Thread(new SpiderDescRuleWorker(i)).start();
		}
		
	}

	/**
	 * @Author Rambo
	 * @Desc：初始化爬取的URL
	 * @param url
	 *            TODO
	 * @Version SpiderRuleRun.java,v 1.1 2014-9-17 上午11:17:00
	 */
	private static void initializeQueue(SysSpiderRuleBean[] sysSpiderRuleBeans) {
		// UrlQueue.addElement("http://blog.sina.com.cn/s/blog_4548a53d0100nydy.html");
		for (int i = 0; i < sysSpiderRuleBeans.length; i++) {
			SpiderRuleQueue.addElement(sysSpiderRuleBeans[i]);
		}
	}

	/**
	 * 初始化配置文件参数
	 */
	private static void initializeParams() {
		InputStream in;
		try {
			in = new BufferedInputStream(new FileInputStream(
					SpiderDescRuleRun.class.getResource("/").getPath()
							+ "/config/spider/spider.properties"));

			Properties properties = new Properties();
			properties.load(in);

			// 从配置文件中读取参数
			SpiderParams.WORKER_NUM = Integer.parseInt(properties
					.getProperty("spider.threadNum"));
			SpiderParams.DEYLAY_TIME = Integer.parseInt(properties
					.getProperty("spider.fetchDelay"));

			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
